//package com.example.financechain.service.Impl;
//
//import com.example.financechain.Utils.GatewayUtilss;
//import com.example.financechain.Utils.ResultCode;
//import com.example.financechain.Utils.UserThreadLocal;
//import com.example.financechain.domain.BlockInfomation;
//import com.example.financechain.domain.entity.Role;
//import com.example.financechain.service.IBlockService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.codec.binary.Hex;
//import org.hyperledger.fabric.gateway.Gateway;
//import org.hyperledger.fabric.gateway.Network;
//import org.hyperledger.fabric.sdk.BlockInfo;
//import org.hyperledger.fabric.sdk.Channel;
//import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
//import org.hyperledger.fabric.sdk.exception.ProposalException;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.security.InvalidKeyException;
//import java.security.cert.CertificateException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//
//@Slf4j
//@Service
//public class BlockServiceImpl implements IBlockService {
//
//    @Override
//    public BlockInfomation queryBlockinfo(int number) {
//        BlockInfomation blockInfomation2 = new BlockInfomation();
//        // Connect to the network using the gateway
//        Role role = UserThreadLocal.get();
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            BlockInfo blockInfo = channel.queryBlockByNumber(number);
//
//
//
//
//            Iterable<BlockInfo.EnvelopeInfo> envelopeInfos = blockInfo.getEnvelopeInfos();
//            String txid ;
//            for (BlockInfo.EnvelopeInfo envelopeInfo : envelopeInfos) {
//                //资源消耗太大，后期关掉
//                System.out.println("Transaction ID: " + envelopeInfo.getTransactionID());
//                txid = envelopeInfo.getTransactionID();
//            }
//
//            blockInfomation2.setNumber(blockInfo.getBlockNumber());
//            blockInfomation2.setPrehash(Hex.encodeHexString(blockInfo.getPreviousHash()));
//            blockInfomation2.setHash(Hex.encodeHexString(blockInfo.getDataHash()));
//
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocks: " + e);
//
//
//        }
//        return blockInfomation2;
//    }
//
//    @Override
//    public List<BlockInfomation> queryBlock() {
//        // Connect to the network using the gateway
//        Role role =new Role();
//        role.setRoleCertification("Org1MSP");
//        role.setRoleName("武汉学院");
//
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            List<BlockInfomation> blockInfomationList =new ArrayList<>();
//            for (int i = 0; i < blockCount; i++) {
//                BlockInfo blockInfo = channel.queryBlockByNumber(i);
//                Iterable<BlockInfo.EnvelopeInfo> envelopeInfos = blockInfo.getEnvelopeInfos();
//                String txid = null;
//                for (BlockInfo.EnvelopeInfo envelopeInfo : envelopeInfos) {
//                    //资源消耗太大，后期关掉
//                    log.info("Transaction ID: " + envelopeInfo.getTransactionID());
//                     txid = envelopeInfo.getTransactionID();
//                }
//                BlockInfomation blockInfomation =new BlockInfomation();
//                blockInfomation.setNumber(blockInfo.getBlockNumber());
//                blockInfomation.setPrehash(Hex.encodeHexString(blockInfo.getPreviousHash()));
//                blockInfomation.setHash(Hex.encodeHexString(blockInfo.getDataHash()));
//                blockInfomation.setTxid(txid);
//
//                blockInfomationList.add(blockInfomation);
//
//            }
//
//            return blockInfomationList;
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocklist: " + e);
//
//        }
//
//        return null;
//
//    }
//
//    @Override
//    public Long queryheight() {
//
//            Long lasteheight = null;
//        Role role = UserThreadLocal.get();
//            try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//                Network network = gateway.getNetwork("energycarchannel");
//
//                Channel channel = network.getChannel();
//
//                lasteheight= channel.queryBlockchainInfo().getHeight();
//
//
//            } catch (Exception e) {
//                System.err.println("Failed to query blocks: " + e);
//
//            }
//            return lasteheight;
//    }
//
//    @Override
//    public List<String> querDetail(int number) {
//        List<String> list = new ArrayList<>();
//        BlockInfomation blockInfomation2 = new BlockInfomation();
//        // Connect to the network using the gateway
//        Role role = UserThreadLocal.get();
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            BlockInfo blockInfo = channel.queryBlockByNumber(number);
//
//
//            Iterable<BlockInfo.EnvelopeInfo> envelopeInfos = blockInfo.getEnvelopeInfos();
//            String txid = null;
//            for (BlockInfo.EnvelopeInfo envelopeInfo : envelopeInfos) {
//                System.out.println("Transaction ID: " + envelopeInfo.getTransactionID());
//                txid = envelopeInfo.getTransactionID();
//            }
//
//            if(txid==null){
//                return null;
//            }
//            HashMap<String, Object> blockmap = ResultCode.queryFromBlockByTxId(txid, channel);
//
//            System.out.println(blockmap);
//
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocks: " + e);
//
//        }
//        return list;
//    }
//
//    @Override
//    public List<String> querDetailbyTXID(String txid) {
//        List<String> list = new ArrayList<>();
//        BlockInfomation blockInfomation2 = new BlockInfomation();
//        // Connect to the network using the gateway
//        Role role = UserThreadLocal.get();
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            //BlockInfo blockInfo = channel.queryBlockByNumber(number);
//
//
//            HashMap<String, Object> blockmap = ResultCode.queryFromBlockByTxId(txid, channel);
//
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocks: " + e);
//
//
//        }
//        return list;
//    }
//}
